package com.tappytaps.android.hc2010;

import java.util.ArrayList;
import java.util.List;

import com.google.api.translate.Language;
import com.tappytaps.android.hc2010.helper.TranslatorHelper;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TestingActivity extends VoiceRecognitionActivity {
	
	private int currentWordIndex = 0;
	private List<Word> wordsList = new ArrayList<Word>();
	int status;
	
	TextView tvOriginal;
	TextView etAnswer;
	Button btnTestMe;
	Button btnRecordText;
	TranslatorHelper translationHelper;
	
	Word word;
	
    @Override
	protected void onPause() {
		super.onPause();		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	translationHelper = new TranslatorHelper(Language.ENGLISH, Language.FRENCH, this);
    	super.onCreate(savedInstanceState);
    	status = 0;
        setContentView(R.layout.testing_ativity);
                
    	tvOriginal = (TextView)findViewById(R.id.tvOriginal);
    	etAnswer = (TextView)findViewById(R.id.etAnswer);
    	btnTestMe = (Button)findViewById(R.id.btnTestMe);
    	//btnTestMe.setText("Show answer!");
    	btnRecordText = (Button)findViewById(R.id.btnRecordText);
    	btnRecordText.setOnClickListener(new OnClickListener() {	
			public void onClick(View v) {
				startVoiceRecognitionActivity("Say it!", "fr");
			}
		});
    	
    	
    	btnTestMe.setOnClickListener(new OnClickListener() {			

			public void onClick(View v) {
				if (status == 1) {
					showNext();
					status = 0;
				} else {
					//btnTestMe.setText("Next question!");
					displayReply();
					status = 1;
				}
				
			}
		});
    	    	
        loadWords();
        showNext();
    }
    
    private void displayReply() {
    	etAnswer.setText(word.translation);
    	translationHelper.sayInDestination(word.translation);
    }
    
    private void loadWords() {
    	Database db = new Database(this);
    	db.open();
    	
    	Cursor lwords = db.fetchAllWords();
    	
    	int indexRowId = lwords.getColumnIndex(Database.KEY_WORD_ROWID);
		int indexOriginal = lwords.getColumnIndex(Database.KEY_WORD_ORIGINAL);
		int indexTranslation = lwords.getColumnIndex(Database.KEY_WORD_TRANSLATION);
		int indexPriority = lwords.getColumnIndex(Database.KEY_WORD_PRIORITY);
		int indexIdCategory = lwords.getColumnIndex(Database.KEY_WORD_ID_CATEGORY);
		int indexLastUpdate = lwords.getColumnIndex(Database.KEY_WORD_LAST_UPDATE);
		int indexCreated = lwords.getColumnIndex(Database.KEY_WORD_CREATED);
	
		long rowId;
		String original, translation;
		int priority;
		int idCategory;
		long lastUpdate, created;
	
    	
    	lwords.moveToFirst();
		while (!lwords.isAfterLast()) {
			rowId = lwords.getLong(indexRowId); 
			original = lwords.getString(indexOriginal);
			translation = lwords.getString(indexTranslation);
			priority = lwords.getInt(indexPriority);
			idCategory = lwords.getInt(indexIdCategory);
			lastUpdate = lwords.getLong(indexLastUpdate);
			created = lwords.getLong(indexCreated);
			
			wordsList.add(new Word(rowId, original, translation, priority, idCategory));
			lwords.moveToNext();
		}
    	
    	db.close();
    }
    
    private boolean showNext() {
    	if (hasNext()) {
    		etAnswer.setBackgroundColor(Color.TRANSPARENT);
    		word = wordsList.get(currentWordIndex);
    		// add answear
    		tvOriginal.setText(word.original);
    		
    		// delete 
    		etAnswer.setText("");

    		currentWordIndex++;
    		return true;
    	} else {
    		return false;
    	}
    	
    }
    
    private boolean hasNext() {
    	return wordsList.size() > currentWordIndex;
    }

	@Override
	void voiceRecognized(String s) {
		etAnswer.setText(word.translation);
		if (s.equals(word.translation)) {
			etAnswer.setBackgroundColor(Color.GREEN);
		} else {
			etAnswer.setBackgroundColor(Color.RED);
		}
		translationHelper.sayInDestination(word.translation);
	}
    
    
}
